package weatherwidget.isrefluxer.bird.com.birdweatherwidget.until;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import weatherwidget.isrefluxer.bird.com.birdweatherwidget.constant.WeatherConstant;
import weatherwidget.isrefluxer.bird.com.birdweatherwidget.data.WeatherInfo;

/**
 * Created by Administrator on 2016/11/22.
 */

public class WeatherUtil {

    private final static String TAG = "WeatherUtil";
    public static ArrayList<WeatherInfo> parseWeaherInfo(String s) throws JSONException{
        ArrayList<WeatherInfo> weatherinfos = new ArrayList<WeatherInfo>();
         LogUtil.log(TAG,s);
        JSONObject jsonobj = new JSONObject(s);
        int status = jsonobj.getInt("status");
        LogUtil.log(TAG,"status = "+status);
        if(status==WeatherConstant.WEATHER_STATUS_OK){
            LogUtil.log(TAG,"infocode = "+jsonobj.getInt("infocode"));
            if(jsonobj.getInt("infocode")== WeatherConstant.WEATHER_INFO_ECODE_OK)
            {
                JSONArray jsonArray = jsonobj.getJSONArray("lives");
                int length = jsonArray.length();
                JSONObject json;
                WeatherInfo weatherInfo;

                for (int i=0;i<length;i++){
                    json = jsonArray.getJSONObject(i);
                    weatherInfo = new WeatherInfo();
                    LogUtil.log(TAG,"reporttime = "+json.getString("reporttime").split(" ")[0]);
                    weatherInfo.setValueByKey(WeatherInfo.DATE, json.getString("reporttime").split(" ")[0]);
                    weatherInfo.setValueByKey(WeatherInfo.CITY_NAME,
                            json.getString("city"));
//                    JSONArray js = json.getJSONArray("casts");
//                    JSONObject info = null ;
//                    if(js.length()>0)
//                    {
//                        info = js.getJSONObject(0);
//                        temperature = json.getString("nighttemp");
//                        weatherInfo.setValueByKey(WeatherInfo.NOWTEMPER,
//                                temperature);
//
//                    }
                    weatherInfo.setValueByKey(WeatherInfo.NOWTEMPER,
                                json.getString("temperature"));
                    weatherInfo.setValueByKey(WeatherInfo.WIND, json.getString("winddirection"));
                    LogUtil.log(TAG,"winddirection = "+json.getString("winddirection"));
                    weatherInfo.setValueByKey(WeatherInfo.WIND_POWER, getWindPower(json.getInt("windpower")));
                    weatherInfo.setValueByKey(WeatherInfo.WEATHER_DAY,json.getString("weather"));
                    weatherInfo.setValueByKey(WeatherInfo.WEATHER_NIGHT,json.getString("weather"));
                    weatherinfos.add(weatherInfo);
                }
            }

        }

        return weatherinfos;


    }

    private static String getWindPower(int windpower) {
         switch (windpower){
             case 0:
                 return "<=3级";
             case 1:
                 return "4级";
             case 2:
                 return "5级";
             case 3:
                 return "6级";
             case 4:
                 return "7级";
             case 5:
                 return "8级";
             case 6:
                 return "9级";
             case 7:
                 return "10级";
             case 8:
                 return "11级";
             case 9:
                 return "12级";

         }
        return "<=3级";
    }


}
